path <- "/var/www/cgi-bin/r/web_calc_portfolio_performance.r"
print( path )
source( path )


test_create_earning_rates_000 <- function()
{
	print( "test case 000" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
#	prices_by_code <- list()
	index <- 1
	codes <- c()
	sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
	sql <- paste( sql, "1301' order by exchange_date limit 10;", sep="" )
	res <- dbGetQuery( conn, sql )
	prices_by_code[[ index ]] <- res
	dbDisconnect( conn )

	res <- create_earning_rates( prices_by_code, 1 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code

#	earning_rates <- create_earning_rates <- create_earning_rates( prices_by_code, 1 )
#	print( earning_rates )
#	print( dates_by_code )

	assert_equals( 1, length( earning_rates ), "# 1, length( earning_rates) " )
	assert_equals( 8, length( earning_rates[[ 1 ]] ), "# 8, length( earning_rates[[ 1 ]] )" )
	expected <- c( 0.085106383, 0.045751634, -0.031250000, -0.019354839,
				   0.071895425, -0.024390244, -0.012500000, -0.006329114 )
	index = 1
	for( value in expected )
	{
		assert_numeric_equals( value, earning_rates[[ 1 ]][ index ], 0.00000001,
			"# value, earning_rates[[ 1 ]][ index ], 0.000001" )
		index <- index + 1
	}
#	次のデータが取得されている
#1  1983-01-04   141
#2  1983-01-05   153
#3  1983-01-06   160
#4  1983-01-07   155
#5  1983-01-08   152
#6  1983-01-10   153
#7  1983-01-11   164
#8  1983-01-12   160
#9  1983-01-13   158
#10 1983-01-14   157
}

test_create_earning_rates_001 <- function()
{
	print( "test case 001" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
#	prices_by_code <- list()
	index <- 1
	codes <- c()
	sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
	sql <- paste( sql, "1301' order by exchange_date limit 10;", sep="" )
	res <- dbGetQuery( conn, sql )
	prices_by_code[[ index ]] <- res
	dbDisconnect( conn )

	res <- create_earning_rates( prices_by_code, 2 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code

#	earning_rates <- create_earning_rates <- create_earning_rates( prices_by_code, 2 )
#	print ( earning_rates )

	assert_equals( 1, length( earning_rates ), "# 1, length( earning_rates) " )
	assert_equals( 7, length( earning_rates[[ 1 ]] ), "# 7, length( earning_rates[[ 1 ]] )" )
	expected <- c( 0.134751773, 0.013071895, -0.050000000,  0.006578947,
				   0.045751634, -0.036585366, -0.018750000 )

	index = 1
	for( value in expected )
	{
		assert_numeric_equals( value, earning_rates[[ 1 ]][ index ], 0.00000001,
			"# value, earning_rates[[ 1 ]][ index ], 0.000001" )
		index <- index + 1
	}
#	次のデータが取得されている
#1  1983-01-04   141
#2  1983-01-05   153
#3  1983-01-06   160
#4  1983-01-07   155
#5  1983-01-08   152
#6  1983-01-10   153
#7  1983-01-11   164
#8  1983-01-12   160
#9  1983-01-13   158
#10 1983-01-14   157
}

test_create_earning_rates_002 <- function()
{
	print( "test case 002" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
#	prices_by_code <- list()
	index <- 1
	codes <- c()
	sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
	sql <- paste( sql, "1301' order by exchange_date limit 10;", sep="" )
	res <- dbGetQuery( conn, sql )
	prices_by_code[[ index ]] <- res
	dbDisconnect( conn )

	res <- create_earning_rates( prices_by_code, 3 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code
	
#	print ( earning_rates )

	assert_equals( 1, length( earning_rates ), "# 1, length( earning_rates) " )
	assert_equals( 6, length( earning_rates[[ 1 ]] ), "# 6, length( earning_rates[[ 1 ]] )" )
	expected <- c( 0.099290780, -0.006535948, -0.012903226,  0.078947368,
				   0.032679739 )

	index = 1
	for( value in expected )
	{
		assert_numeric_equals( value, earning_rates[[ 1 ]][ index ], 0.00000001,
			"# value, earning_rates[[ 1 ]][ index ], 0.000001" )
		index <- index + 1
	}
#	次のデータが取得されている
#1  1983-01-04   141
#2  1983-01-05   153
#3  1983-01-06   160
#4  1983-01-07   155
#5  1983-01-08   152
#6  1983-01-10   153
#7  1983-01-11   164
#8  1983-01-12   160
#9  1983-01-13   158
#10 1983-01-14   157
}

test_create_earning_basic_statics_lists_003 <- function()
{
	print( "test case 003" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
	index <- 1
	codes <- c( "1301", "9996" )
	for ( code in codes )
	{
		sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
		sql <- paste( sql, code, sep="" )
		sql <- paste( sql, "' and '1994-10-1' <= exchange_date order by exchange_date limit 10;", sep="" )
		prices_by_code[[ index ]] <- dbGetQuery( conn, sql )
		index <- index + 1
	}
	dbDisconnect( conn )

#	print( prices_by_code )

#1  1994-10-03   404
#2  1994-10-04   402
#3  1994-10-05   413
#4  1994-10-06   403
#5  1994-10-07   407
#6  1994-10-11   413
#7  1994-10-12   430
#8  1994-10-13   430
#9  1994-10-14   422
#10 1994-10-17   421

#1  1994-10-03 1979.040
#2  1994-10-04 2012.890
#3  1994-10-05 1985.984
#4  1994-10-06 1972.096
#5  1994-10-07 1922.934
#6  1994-10-11 1944.040
#7  1994-10-12 1909.050
#8  1994-10-13 1929.876
#9  1994-10-14 1922.934
#10 1994-10-17 1929.876

	res <- create_earning_rates( prices_by_code, 3 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code

	res <- create_earning_basic_statics_lists( earning_rates, dates_by_code )

	err <- 0.0000001
	assert_equals( 2, length( res$means ), "2, length( res$means )" )
	assert_numeric_equals( 0.003008901, res$means[ 1 ], err, "0.003008901, res$means[ 1 ]" )
	assert_numeric_equals( -0.007656317, res$means[ 2 ], err, "-0.007656317, res$means[ 2 ]" )

	assert_equals( 2, length( res$variances ), "2, length( res$variances )" )
	assert_numeric_equals( 0.0002836308, res$variances[ 1 ], err, "0.0002836308, res$variances[ 1 ]" )
	assert_numeric_equals( 0.0003125958, res$variances[ 2 ], err, "0.0003125958, res$variances[ 2 ]" )

	assert_equals( 2, ncol( res$covariances ), "2, ncol( res$covariances )" )
	assert_equals( 2, nrow( res$covariances ), "2, nrow( res$covariances )" )
	assert_numeric_equals( 0.0002836308, res$covariances[ 1, 1 ], err, "0.0002836308, res$covariances[ 1, 1 ], err" )
	assert_numeric_equals( -0.0001255635, res$covariances[ 2, 1 ], err, "-0.0001255635, res$covariances[ 2, 1 ], err" )
	assert_numeric_equals( -0.0001255635, res$covariances[ 1, 2 ], err, "0.0002836308, res$covariances[ 1, 2 ], err" )
	assert_numeric_equals( 0.0003125958, res$covariances[ 2, 2 ], err, "-0.0001255635, res$covariances[ 2, 2 ], err" )
}

test_create_earning_basic_statics_lists_004 <- function()
{
	print( "test case 004" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
	index <- 1
	codes <- c( "1301", "9996" )
	dates <- c( "1994-10-1", "1994-10-5" )
	for ( code in codes )
	{
		sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
		sql <- paste( sql, code, sep="" )
		sql <- paste( sql, "' and '", sep="" )
		sql <- paste( sql, dates[index], sep="" )
		sql <- paste( sql, "' <= exchange_date order by exchange_date limit 10;", sep="" )
		prices_by_code[[ index ]] <- dbGetQuery( conn, sql )
		index <- index + 1
	}
	dbDisconnect( conn )

#	print( prices_by_code )
	res <- create_earning_rates( prices_by_code, 3 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code

#	print( earning_rates )
#	print( dates_by_code )

	res <- create_earning_basic_statics_lists( earning_rates, dates_by_code )
#	print ( res )

	err <- 0.0000001
	assert_equals( 2, length( res$means ), "2, length( res$means )" )
	assert_numeric_equals( 0.007346166, res$means[ 1 ], err, "0.007346166, res$means[ 1 ]" )
	assert_numeric_equals( -0.003623348, res$means[ 2 ], err, "--0.003623348, res$means[ 2 ]" )

	assert_equals( 2, length( res$variances ), "2, length( res$variances )" )
	assert_numeric_equals( 0.0001418193, res$variances[ 1 ], err, "0.0001418193, res$variances[ 1 ]" )
	assert_numeric_equals( 0.0001046460, res$variances[ 2 ], err, "0.0001046460, res$variances[ 2 ]" )

	assert_equals( 2, ncol( res$covariances ), "2, ncol( res$covariances )" )
	assert_equals( 2, nrow( res$covariances ), "2, nrow( res$covariances )" )
	assert_numeric_equals( 0.0001418193, res$covariances[ 1, 1 ], err, "0.0001418193, res$covariances[ 1, 1 ], err" )
	assert_numeric_equals( -0.0001747709, res$covariances[ 2, 1 ], err, "-0.0001747709, res$covariances[ 2, 1 ], err" )
	assert_numeric_equals( -0.0001747709, res$covariances[ 1, 2 ], err, "-0.0001747709, res$covariances[ 1, 2 ], err" )
	assert_numeric_equals( 0.0001046460, res$covariances[ 2, 2 ], err, "0.0001046460, res$covariances[ 2, 2 ], err" )

}

test_create_earning_basic_statics_lists_005 <- function()
{
	print( "test case 005" )

	conn <- dbConnect( pgSQL( classPath='/usr/local/pgsql/share/java/jdbc4.jar' ), user='jiro', dbname='MarketDatabase' )
	prices_by_code <- as.list( NA )
	index <- 1
	codes <- c( "1301", "9996", "1305" )
	dates <- c( "2001-10-1", "2001-10-5", "2001-10-3" )
	for ( code in codes )
	{
		sql <- "select exchange_date as date, adjusted_end_price as price from stock_exchange_ja_table where code='"
		sql <- paste( sql, code, sep="" )
		sql <- paste( sql, "' and '", sep="" )
		sql <- paste( sql, dates[index], sep="" )
		sql <- paste( sql, "' <= exchange_date order by exchange_date limit 10;", sep="" )
		prices_by_code[[ index ]] <- dbGetQuery( conn, sql )
		index <- index + 1
	}
	dbDisconnect( conn )

#	print( prices_by_code )

	res <- create_earning_rates( prices_by_code, 3 )
	earning_rates <- res$rates_by_code
	dates_by_code <- res$dates_by_code

#	print( earning_rates )
#	print( as.Date( dates_by_code[[1]], origin="1970-1-1" ) )
#	print( as.Date( dates_by_code[[2]], origin="1970-1-1" ) )
#	print( as.Date( dates_by_code[[3]], origin="1970-1-1" ) )

	res <- create_earning_basic_statics_lists( earning_rates, dates_by_code )

#	print ( res )

	err <- 0.0000001
	assert_equals( 3, length( res$means ), "3, length( res$means )" )
	assert_numeric_equals( 0.02695435, res$means[ 1 ], err, "0.02695435, res$means[ 1 ]" )
	assert_numeric_equals( 0.02473958, res$means[ 2 ], err, "0.02473958, res$means[ 2 ]" )
	assert_numeric_equals( 0.01456689, res$means[ 3 ], err, "0.01456689, res$means[ 3 ]" )

	assert_equals( 3, length( res$variances ), "3, length( res$variances )" )
	assert_numeric_equals( 0.0007701906, res$variances[ 1 ], err, "0.0007701906, res$variances[ 1 ]" )
	assert_numeric_equals( 0.0035157606, res$variances[ 2 ], err, "0.0035157606, res$variances[ 2 ]" )
	assert_numeric_equals( 0.0012206879, res$variances[ 3 ], err, "0.0012206879, res$variances[ 3 ]" )

	assert_equals( 3, ncol( res$covariances ), "3, ncol( res$covariances )" )
	assert_equals( 3, nrow( res$covariances ), "3, nrow( res$covariances )" )
	assert_numeric_equals( 0.0007701906, res$covariances[ 1, 1 ], err, "0.0007701906, res$covariances[ 1, 1 ], err" )
	assert_numeric_equals( 0.000000000, res$covariances[ 1, 2 ], err, "0.000000000, res$covariances[ 1, 2 ], err" )
	assert_numeric_equals( 0.001560350, res$covariances[ 1, 3 ], err, "0.001560350, res$covariances[ 1, 3 ], err" )
	assert_numeric_equals( 0.0000000000, res$covariances[ 2, 1 ], err, "0.0000000000, res$covariances[ 2, 1 ], err" )
	assert_numeric_equals( 0.003515761, res$covariances[ 2, 2 ], err, "0.003515761, res$covariances[ 2, 2 ], err" )
	assert_numeric_equals( 0.000000000, res$covariances[ 2, 3 ], err, "0.000000000, res$covariances[ 2, 3 ], err" )
	assert_numeric_equals( 0.0015603495, res$covariances[ 3, 1 ], err, "0.0015603495, res$covariances[ 3, 1 ], err" )
	assert_numeric_equals( 0.000000000, res$covariances[ 3, 2 ], err, "0.000000000, res$covariances[ 3, 2 ], err" )
	assert_numeric_equals( 0.001220688, res$covariances[ 3, 3 ], err, "0.001220688, res$covariances[ 3, 3 ], err" )

	investment_rates <- c( 0.5, 0.2, 0.3 )
#	res <- calc_portfolio_performance( res$means, res$variances, res$covariances, investment_rates )
#	print( res )
}

#run test
test_create_earning_rates_000()
test_create_earning_rates_001()
test_create_earning_rates_002()
test_create_earning_basic_statics_lists_004()
test_create_earning_basic_statics_lists_005()

